package cn.lyjuan.dwz_manage.core.java.action;


import cn.lyjuan.dwz_manage.core.java.cst.DwzCst;
import cn.lyjuan.dwz_manage.core.java.exception.ErrorCode;
import cn.lyjuan.dwz_manage.core.java.util.ActionUtils;
import cn.lyjuan.dwz_manage.core.java.util.NumberUtils;
import cn.lyjuan.dwz_manage.core.java.util.SpringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ly on 2015/1/12.
 */
public class BaseAction
{
    private static Logger log = LogManager.getLogger(BaseAction.class.getName());

    /**
     * 获取输出流
     *
     * @param resp
     * @return
     */
    protected OutputStream getOutStream(HttpServletResponse resp)
    {
        OutputStream out = null;
        try
        {
            out = resp.getOutputStream();
        } catch (IOException e)
        {
            log.error("Method[getOutStream] 获取输出流异常", e);
        }
        return out;
    }

    /**
     * 设置默认编码，并获取输出流
     *
     * @param resp
     * @return
     */
    protected PrintWriter getOutPrint(HttpServletResponse resp)
    {
        PrintWriter out = null;
        try
        {
            out = resp.getWriter();
        } catch (IOException e)
        {
            log.error("Method[getOutPrint] 获取输出流异常", e);
        }

        return out;
    }

    /**
     * 整合 GET 和 POST 请求，若有重复， GET 参数会被覆盖
     *
     * @return
     * @throws IOException
     */
    protected Map<String, String> paramsToMap()
    {
        HttpServletRequest req = SpringUtils.getRequest();
        // GET
        Map<String, String> getParams = new HashMap<String, String>();

        Enumeration<String> names = req.getParameterNames();

        if (null == names) return getParams;

        String name  = null;
        String value = null;
        while (names.hasMoreElements())
        {
            name = names.nextElement();

            if (StringUtils.isEmpty(name)) continue;

            value = req.getParameter(name);

            getParams.put(name, value);
        }

        return getParams;
    }

    /**
     * 获取 DWZ 后台当前页参数
     *
     * @return
     */
    public int getDwzPn()
    {
        String pn = SpringUtils.getRequest().getParameter(DwzCst.pagePnName);

        return NumberUtils.isNumber(pn) ? Integer.parseInt(pn) : 1;
    }

    /**
     * 获取 DWZ 后台页大小参数
     *
     * @return
     */
    public int getDwzPs()
    {
        String ps = SpringUtils.getRequest().getParameter(DwzCst.pagePsName);
        return NumberUtils.isNumber(ps) ? Integer.parseInt(ps) : DwzCst.pagePsDefaultValue;
    }

    /**
     * 封装后台分页参数
     *
     * @param totalCount 总分页数
     */
    public void pkgManagePage(int totalCount)
    {
        HttpServletRequest req = SpringUtils.getRequest();

        if (null == req) return;

        req.setAttribute(DwzCst.pageTotalcountName, totalCount);
        req.setAttribute(DwzCst.pagePnName, getDwzPn());
        req.setAttribute(DwzCst.pagePsName, getDwzPs());
    }

    public void returnDwzAjaxFail(int code, String msg)
    {
        HttpServletRequest req = SpringUtils.getRequest();

        HttpServletResponse resp = SpringUtils.getResponse();

        ActionUtils.returnDwzAjax(req, resp, code, msg);
    }

    public void returnDwzAjaxFail(String msg)
    {
        returnDwzAjaxFail(ErrorCode.Mg.ERR, msg);
    }

    public void returnDwzAjaxSucc(String msg)
    {
        HttpServletRequest req = SpringUtils.getRequest();

        HttpServletResponse resp = SpringUtils.getResponse();

        ActionUtils.returnDwzAjax(req, resp, ErrorCode.Mg.SUCC, msg);
    }

    public void returnDwzAjaxSucc()
    {
        HttpServletRequest req = SpringUtils.getRequest();

        HttpServletResponse resp = SpringUtils.getResponse();

        ActionUtils.returnDwzAjax(req, resp, ErrorCode.Mg.SUCC, "操作成功");
    }

    public void returnDwzAjaxSuccAndData(Object data)
    {
        HttpServletRequest req = SpringUtils.getRequest();

        HttpServletResponse resp = SpringUtils.getResponse();

        ActionUtils.returnDwzAjax(req, resp, ErrorCode.Mg.SUCC, "操作成功", data);
    }

    /**
     * 获取当前登陆用户名
     *
     * @return
     */
    public String getUserName()
    {
        return SpringUtils.getUserPrincipal().getUsername();
    }

    /**
     * 获取当前登陆用户权限
     *
     * @return
     */
    public List<SimpleGrantedAuthority> getAuthorities()
    {
        return (List<SimpleGrantedAuthority>) SpringUtils.getUserPrincipal().getAuthorities();
    }

    public BaseAction()
    {
    }
}
